﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMFNet;
using System.Net;

namespace test46
{
    class Monitor2
    {
        private AMFNet.AMFNetServer _Server;

        private Dictionary<Object, Client> clients = new Dictionary<Object, Client>();
        private MapInfo2 aMapInfo = new MapInfo2();
        private int LastUserId;

        public void StartUp()
        {
            LastUserId = 1;
            aMapInfo.ColCount = 100;
            aMapInfo.RowCount = 100;
            aMapInfo.CellWidth = 14;
            aMapInfo.CellHeight = 14;
            aMapInfo.CellValues = "map1.txt";
            aMapInfo.MoveSpeed = 40;
            aMapInfo.StartIndex = 0;

            _Server = new AMFNetServer();
            IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];
            Console.WriteLine("ServerIp:" + ip.ToString());
            _Server.OpenServer(ip, 8888,4096,2000);
            _Server.OnConnected = OnConnected;
            _Server.OnReceive = ReceiveData;
            _Server.OnDisonnected = OnDisonnected;

        }

        public void Stop()
        {
            _Server.CloseServer();
        }
        private void OnDisonnected(Object SocketObject)
        {
            Client aClient = clients[SocketObject];

            clients.Remove(SocketObject);
            RemoveUser aRemoveUser = new RemoveUser();
            aRemoveUser.UserId = aClient.UserId;
            foreach (Client cl in clients.Values)
            {
                _Server.SendMessage(cl.SocketObject, aRemoveUser);
            }

        }
        private void OnConnected(Object SocketObject)
        {
            Client cl = new Client();
            cl.UserId = LastUserId;
            cl.SocketObject = SocketObject;
            LastUserId++;
            clients.Add(SocketObject,cl);
        }
        private void ReceiveData(Object SocketObject, MessageBase msg)
        {
            if (msg.MessageType == 1)
            {
                _Server.SendMessage(SocketObject, aMapInfo);
            }
            else if (msg.MessageType == 3)
            {
                Client ct = clients[SocketObject];
                PathInfo aPathInfo = (PathInfo)msg;
                aPathInfo.UserId = ct.UserId;
                ct.CurPath = aPathInfo;
                ct.StartTime = DateTime.Now;

                foreach (Client cl in clients.Values)
                {
                    if (cl != ct)
                    {
                        _Server.SendMessage(cl.SocketObject, aPathInfo);
                    }

                }
            }
            else if (msg.MessageType == 4)
            {
                Client ct = clients[SocketObject];
                ActorsPosition aActorsPosition = GetActorsPosition(ct);
                _Server.SendMessage(ct.SocketObject, aActorsPosition);
            }
        }

        public ActorsPosition GetActorsPosition(Client SelfClinet)
        {
            ActorsPosition aActorsPosition = new ActorsPosition();
            foreach (Client cl in clients.Values)
            {
                if (cl != SelfClinet && cl.CurPath != null)
                {
                    PathInfo2 aPathInfo2 = new PathInfo2();
                    aPathInfo2.aPathInfo = cl.CurPath;
                    aPathInfo2.EclipseTime = (DateTime.Now - cl.StartTime).TotalSeconds;
                    aActorsPosition.PathInfos.Add(aPathInfo2);
                }
            }
            return aActorsPosition;
        }
    }
}
